package cn.hl.hm.logic.manage;

import cn.hl.hm.dao.RechargeRecoardOfflineMapper;
import cn.hl.hm.dao.RoadFeeCollectorMapper;
import cn.hl.hm.entity.RechargeRecoardOffline;
import cn.hl.hm.entity.RoadFeeCollector;
import cn.hl.hm.logic.service.IRechargeRecoardOfflineService;
import cn.hl.hm.util.List2Map;
import cn.hl.hm.util.OrderUtil;
import cn.hl.hm.util.paging.PageInfo;
import cn.hl.hm.vo.RechargeRecoardOfflineVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.*;

/**
 * 线下保全金逻辑控制
 * Created by xianghan on 18/1/8.
 */
@Service
public class RechargeRecoardOfflineLogic {
    @Autowired
    private IRechargeRecoardOfflineService rechargeRecoardOfflineService;
    @Autowired
    private RechargeRecoardOfflineMapper rechargeRecoardOfflineMapper;
    @Autowired
    private RoadFeeCollectorMapper roadFeeCollectorMapper;


    public Boolean add(String userPhone, String plateNumber, BigDecimal amount,
                       Integer payType, Integer rechargeAdminId, Integer roadFeeCollectorId) {
        try {
            RechargeRecoardOffline rechargeRecoardOffline=new RechargeRecoardOffline();
            rechargeRecoardOffline.setOperType(payType);
            rechargeRecoardOffline.setRechargeCode(OrderUtil.getInstance().getRoadFeeOrderCode());
            rechargeRecoardOffline.setPlateNumber(plateNumber);
            rechargeRecoardOffline.setUserPhone(userPhone);
            rechargeRecoardOffline.setAmount(amount);
            rechargeRecoardOffline.setPayType(payType);
            rechargeRecoardOffline.setCreateTime(new Date());
            rechargeRecoardOffline.setRechargeAdminId(rechargeAdminId);
            rechargeRecoardOffline.setRoadFeeCollectorId(roadFeeCollectorId);
            int rows=rechargeRecoardOfflineService.addRechargeRecoardOffline(rechargeRecoardOffline);
            if (rows>0)
                return true;
            else
                return false;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public List<RechargeRecoardOfflineVo> selectListByPageInfo(PageInfo pageInfo){
        List<RechargeRecoardOffline> rechargeRecoardOfflineList = rechargeRecoardOfflineService.selectRROListByPage(pageInfo);
        if (rechargeRecoardOfflineList!=null) {
            Map<Integer,RoadFeeCollector> roadFeeCollectorMap = List2Map.getInstance()
                    .tobeMap("getId", roadFeeCollectorMapper.selectRoadFeeCollector(new RoadFeeCollector()));
            RoadFeeCollector roadFeeCollector=null;
            RechargeRecoardOfflineVo rechargeRecoardOfflineVo=null;
            List<RechargeRecoardOfflineVo> rechargeRecoardOfflineVos=new ArrayList<>();
            Double totleAmount=0d;
            for (RechargeRecoardOffline rechargeRecoardOffline : rechargeRecoardOfflineList) {
                rechargeRecoardOfflineVo=new RechargeRecoardOfflineVo(rechargeRecoardOffline);
                totleAmount+=rechargeRecoardOfflineVo.getAmount().doubleValue();
                roadFeeCollector = roadFeeCollectorMap.get(rechargeRecoardOffline.getRoadFeeCollectorId());
                if (rechargeRecoardOffline!=null) {
                    rechargeRecoardOfflineVo.setCollectorName(roadFeeCollector.getCollectorName());
                }
                rechargeRecoardOfflineVos.add(rechargeRecoardOfflineVo);
            }
            pageInfo.setDataList(rechargeRecoardOfflineList);
            pageInfo.getParams().put("totleAmount",totleAmount);
            return rechargeRecoardOfflineVos;
        }
        return null;
    }

    public Double getAllAmount(List<RechargeRecoardOfflineVo> rechargeRecoardOfflineList){
        return rechargeRecoardOfflineMapper.selectTotleAmount();
    }


}
